<script setup>
import { ref, reactive, toRaw } from 'vue';
import { service } from '@utils';
import { ElMessage } from 'element-plus';
const emit = defineEmits(['refresh']);
const visible = ref(false);
const formRef = ref();
const title = ref('');
const disabledBool = ref(false);
const state = reactive({
    param: {},
});

const rules = {
    channelCode: [{ required: true, message: '请输入', trigger: 'blur' }],
    channelName: [{ required: true, message: '请输入', trigger: 'blur' }],
    h5Url: [{ required: true, message: '请输入', trigger: 'blur' }],
    price: [{ required: true, message: '请选择', trigger: 'blur' }],
    priceType: [{ required: true, message: '请选择', trigger: 'change' }],
};

const open = (type, item) => {
    title.value = type;
    disabledBool.value = type == '详情';
    state.param = { ...item };
    visible.value = true;
};

const close = () => {
    visible.value = false;
    state.param = {};
};
const exemptionChange = () => {
    state.param.price = '';
};
const sure = async () => {
    formRef.value.validate(valid => {
        if (valid) {
            realSubmit();
        }
    });
};

const realSubmit = async () => {
    let res;
    if (title.value == '编辑') {
        res = await service.channelConfigApi.channelConfigEdit(toRaw(state.param));
    } else {
        res = await service.channelConfigApi.channelConfigAdd(toRaw(state.param));
    }
    if (res) {
        ElMessage.success(title.value + '成功');
        close();
        emit('refresh', true);
    }
};
//把方法暴露给父组件
defineExpose({
    open,
});
</script>

<template>
    <el-dialog v-model="visible" :title="title" center :close-on-click-modal="false" @close="close" destroy-on-close :width="500" top="12vh">
        <section>
            <el-form :model="state.param" :rules="rules" ref="formRef" label-width="100px" label-suffix=":" onsubmit="return false;" :disabled="disabledBool">
                <el-form-item label="渠道名称" prop="channelName">
                    <el-input v-model="state.param.channelName" placeholder="请输入渠道名称" clearable></el-input>
                </el-form-item>
                <el-form-item label="唯一编码" prop="channelCode">
                    <el-input v-model="state.param.channelCode" placeholder="请输入唯一编码" clearable></el-input>
                </el-form-item>
                <el-form-item label="H5链接" prop="h5Url">
                    <el-input v-model="state.param.h5Url" placeholder="请输入H5链接" clearable></el-input>
                </el-form-item>
                <el-form-item label="单价" prop="priceType">
                    <el-radio-group v-model="state.param.priceType" @change="exemptionChange()" defaultVal="1">
                        <div>
                            <el-radio :label="0">UV单价</el-radio>
                            <el-form-item prop="protect" style="width: 150px; margin-right: 32px" v-if="state.param.priceType == 0">
                                <el-input type="number" v-model="state.param.price" :controls="false" style="width: 100%" placeholder="请输入数字" :precision="2" :min="0">
                                    <template #append>元</template>
                                </el-input>
                            </el-form-item>
                        </div>
                        <div>
                            <el-radio :label="1">下单单价</el-radio>
                            <el-form-item prop="price" style="width: 150px; margin-right: 32px" v-if="state.param.priceType == 1">
                                <el-input type="number" v-model="state.param.price" :controls="false" style="width: 100%" placeholder="请输入数字" :precision="2" :min="0">
                                    <template #append>元</template>
                                </el-input>
                            </el-form-item>
                        </div>
                        <div>
                            <el-radio :label="2">实际租用单价</el-radio>
                            <el-form-item prop="price" style="width: 150px; margin-right: 32px" v-if="state.param.priceType == 2">
                                <el-input type="number" v-model="state.param.price" :controls="false" style="width: 100%" placeholder="请输入数字" :precision="2" :min="0">
                                    <template #append>元</template>
                                </el-input>
                            </el-form-item>
                        </div>
                    </el-radio-group>
                </el-form-item>
            </el-form>
        </section>

        <template #footer>
            <span class="flex-align-end">
                <div class="margin-right20">
                    <el-button @click="close">取消</el-button>
                </div>
                <el-button v-if="title != '详情'" type="primary" @click="sure">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>
<style scoped lang="scss">
.el-radio-group {
    align-items: flex-start;
    width: 90%;
    > div {
        width: 100%;
        display: flex;
    }
}
</style>
